import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/MainHeader/MainHome/MainHome.vue'),
    // 是否显示头部 和 尾部
    meta: { header: true,footer:true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/MainHeader/MainLogin/MainLogin.vue'),
    meta: { header: true,footer: true}
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/MainHeader/MainLogin/MainRegister.vue'),
    meta: { header: true,footer: true}
  },
  {
    path: '/forgot',
    name: 'Forgot',
    component: () => import('../views/MainHeader/MainLogin/MainForgotPwd.vue'),
    meta: { header: true,footer: true}
  },
  {
    path: '/plat-tools',
    component: () => import('../views/PlatTools/PlatToolsMain.vue'),
    redirect: '/plat-tools/project-manage',
    children: [
      {
        path: 'project-manage',
        name: 'ProjectManage',
        component: () => import('../views/PlatTools/ProjectManageTool/ProjectManageTool.vue')
      },
      {
        path: 'budget-form',
        name: 'BudgetForm',
        component: () => import('../views/PlatTools/BudgetFormTool/BudgetFormTool.vue')
      },
      {
        path: 'script',
        name: 'Script',
        component: () => import('../views/PlatTools/ScriptTool/ScriptTool.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path.startsWith('/plat-tools')) {
    const isLogin = !!localStorage.getItem('user');
    if (!isLogin) {
      next({ path: '/login' });
      return;
    }
  }
  next();
});

export default router
